<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="题目题干" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="topicTitle">
              <a-input v-model="model.topicTitle" placeholder="请输入题目题干"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="专项类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="specialType">
              <j-category-select v-model="model.specialType" pcode="A01" placeholder="请选择专项类别"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="题型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="topicType">
              <j-dict-select-tag type="list" v-model="model.topicType" dictCode="topic_type" placeholder="请选择题型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.topicType === '3'">
            <a-form-model-item label="对错" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isRight">
              <j-dict-select-tag type="radio" v-model="model.isRight" dictCode="topic_right" placeholder="请选择对错" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="答案解析" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="answer">
              <a-textarea v-model="model.answer" rows="4" placeholder="请输入答案解析" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="分数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="score">
              <a-input-number v-model="model.score" placeholder="请输入分数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否必练题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="must">
              <j-dict-select-tag type="radio" v-model="model.must" dictCode="topic_pay" placeholder="请选择是否必练题" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="radio" v-model="model.status" dictCode="topic_status" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="题目配图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="topicPicture">
              <j-upload v-model="model.topicPicture"   ></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否付费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isPay">
              <j-dict-select-tag type="radio" v-model="model.isPay" dictCode="topic_pay" placeholder="请选择是否付费" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="题目类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="topicCategory">
              <j-category-select v-model="model.topicCategory" pcode="B02" :multiple=true placeholder="请选择题目类别"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MftTopicForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           specialType: [
              { required: true, message: '请输入专项类别!'},
           ],
           topicType: [
              { required: true, message: '请输入题型!'},
           ],
           status: [
              { required: true, message: '请输入状态!'},
           ],
           isPay: [
              { required: true, message: '请输入是否付费!'},
           ],
           isRight: [
              { required: true, message: '请输入对错!'},
           ],
           topicCategory: [
              { required: true, message: '请输入题目类别!'},
           ],
        },
        url: {
          add: "/mfttopic/mftTopic/add",
          edit: "/mfttopic/mftTopic/edit",
          queryById: "/mfttopic/mftTopic/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      handleCategoryChange(value,backObj){
         this.model = Object.assign(this.model, backObj);
      }
    }
  }
</script>